package com.drama.api.sys.download.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.sys.download.dao.VideoDownloadExamineDao;
import com.drama.api.sys.download.domain.po.VideoDownloadExaminePO;
import com.drama.api.sys.download.domain.vo.*;
import com.drama.api.sys.download.service.IVideoDownloadExamineService;
import com.drama.api.sys.dramas.dao.SysVideoDao;
import com.drama.api.sys.dramas.domain.po.SysVideoPO;
import com.drama.api.sys.log.dao.SysLogDao;
import com.drama.api.task.SysLogThread;
import com.drama.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
@Transactional
@Slf4j
public class VideoDownloadExamineServiceImpl implements IVideoDownloadExamineService {

    @Autowired
    private VideoDownloadExamineDao examineDao;
    @Autowired
    private SysLogDao sysLogDao;
    @Autowired
    SysVideoDao sysVideoDao;

    @Override
    public List<DownloadExamineVO> getList(DownloadExamineSearchVO searchVO) {
        return examineDao.getList(searchVO);
    }

    @Override
    public void examine(DownloadExamineStateVO vo) {
        VideoDownloadExaminePO videoDownloadExaminePO=examineDao.selectByPrimaryKey(vo.getId());
        if(videoDownloadExaminePO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        if(videoDownloadExaminePO.getDownloadState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"当前状态不能审核！");
        }
        videoDownloadExaminePO.setDownloadState(vo.getDownloadState());
        videoDownloadExaminePO.preUpdate();
        examineDao.updateByPrimaryKeySelective(videoDownloadExaminePO);

        //更新视频信息
        SysVideoPO sysVideoPO=sysVideoDao.selectByPrimaryKey(videoDownloadExaminePO.getVideoId());
        sysVideoPO.setVideoDownloadState(vo.getDownloadState()==2?3:1);
        sysVideoPO.preUpdate();
        sysVideoDao.updateByPrimaryKeySelective(sysVideoPO);
        new SysLogThread(sysLogDao, Constants.SYS_LOG_MODULE[6],(vo.getDownloadState()==2?"通过":"驳回")+"下载申请，申请ID:"+videoDownloadExaminePO.getId(), (String) StpUtil.getLoginId()).start();
    }

    @Override
    public void examineBatch(DownloadExamineStateBatchVO vo) {
        if(vo.getIds()!=null&&vo.getIds().size()>0){
            for (String id:vo.getIds()){
                DownloadExamineStateVO downloadExamineStateVO=new DownloadExamineStateVO();
                downloadExamineStateVO.setDownloadState(vo.getDownloadState());
                downloadExamineStateVO.setId(id);
                this.examine(downloadExamineStateVO);
            }
        }
    }

    @Override
    public void addExamine(DownloadExamineAddVO vo) {
        VideoDownloadExaminePO videoDownloadExaminePO=new VideoDownloadExaminePO();
        //更新视频信息
        SysVideoPO sysVideoPO=sysVideoDao.selectByPrimaryKey(vo.getVideoId());
        if(sysVideoPO==null){
            throw new BusinessException(Type.NOT_FOUND_ERROR);
        }
        if(sysVideoPO.getVideoDownloadState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"当前状态不能申请审核！");
        }
        sysVideoPO.setVideoDownloadState(2);
        sysVideoPO.preUpdate();
        sysVideoDao.updateByPrimaryKeySelective(sysVideoPO);
        //添加审核记录
        videoDownloadExaminePO.setVideoId(sysVideoPO.getId());
        videoDownloadExaminePO.setVideoName(sysVideoPO.getVideoName());
        videoDownloadExaminePO.setVideoRef(sysVideoPO.getVideoRef());
        videoDownloadExaminePO.setDownloadState(1);
        videoDownloadExaminePO.setDownloadRef((examineDao.selectCountByExample(new Example(VideoDownloadExaminePO.class))+1001)+"");
        videoDownloadExaminePO.preInsert();
        examineDao.insertSelective(videoDownloadExaminePO);
    }
}